#include "TextDiff.h"

namespace TextDiffAlgo
{
	TextDiff::TextDiff(DiffType in_DiffType, int in_DiffStartIndex, int in_DiffLength)
	{
		this->setDiffType(in_DiffType);
		this->setDiffStartIndex(in_DiffStartIndex);
		this->setDiffLength(in_DiffLength);
	}

	void TextDiff::setDiffType(DiffType in_DiffType)
	{
		ASSERT((in_DiffType == TYPE_DELETED
			|| in_DiffType == TYPE_INSERTED
			|| in_DiffType == TYPE_SAME_BEFORE
			|| in_DiffType == TYPE_SAME_AFTER
			), "Unknown difference type.");
		m_DiffType = in_DiffType;
	}

	std::string TextDiff::toString()
	{
		std::string res;
		res.clear();

		if (m_DiffType == TYPE_INSERTED) {
			res = "Insert: ";
		} else if (m_DiffType == TYPE_DELETED) {
			res = "Delete: ";
		} else if (m_DiffType == TYPE_SAME_BEFORE){
			res = "RemainA: ";
		} else if (m_DiffType == TYPE_SAME_AFTER){
			res = "RemainB: ";
		}
		res += "from " + Utility::IntToString(m_DiffStartIndex) + " to " + Utility::IntToString(m_DiffStartIndex + m_DiffLength);

		return res;
	}

	int TextDiff::compareTo(const TextDiff& comp)
	{
		int result = 0;
		if (comp.getDiffStartIndex() == this->getDiffStartIndex()) {
			result = (this->getDiffType() == TYPE_DELETED ? -1 : (comp.getDiffType()));
		} else {
			result = this->getDiffStartIndex() - comp.getDiffStartIndex();
		}

		return result;
	}

}